package com.example.demo.config;

import dev.langchain4j.community.model.dashscope.QwenStreamingChatModel;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.DocumentSplitter;
import dev.langchain4j.data.document.loader.ClassPathDocumentLoader;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.splitter.DocumentSplitters;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;

import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.List;

@Configuration
public class AiConfig {

    @Value("${ai.memory.max-messages:20}")
    private int maxMessages;
    
    @Value("${langchain4j.community.dashscope.streaming-chat-model.api-key}")
    private String apiKey;
    
    @Value("${langchain4j.community.dashscope.streaming-chat-model.model-name}")
    private String modelName;
    
    @Value("${ai.model.temperature:0.7}")
    private float temperature;
    
    @Value("${ai.model.top-p:0.9}")
    private double topP;

    @Autowired
    private ChatMemoryStore RedisChatMemoryStore;

    @Autowired
    private EmbeddingModel embeddingModel;




    @Bean
    public ChatMemoryProvider chatMemoryProvider(){
        return sessionId -> MessageWindowChatMemory.builder()
                .id(sessionId)
                .maxMessages(maxMessages)
                .chatMemoryStore(RedisChatMemoryStore)
                .build();
    }
    
    /**
     * 自定义配置通义千问流式聊天模型，覆盖默认配置
     * 使用不同的Bean名称避免冲突
     */
    @Bean("customQwenStreamingChatModel")
    @Primary
    public QwenStreamingChatModel customQwenStreamingChatModel() {
        return QwenStreamingChatModel.builder()
                .apiKey(apiKey)
                .modelName(modelName)
                .temperature(temperature)
                .topP(topP)
                .build();
    }

    @Bean("firstEmbeddingStore")
    @Primary
    public EmbeddingStore embeddingStore(){
        List<Document> documents = ClassPathDocumentLoader.loadDocuments("aiPrompt");

        InMemoryEmbeddingStore embeddingStore = new InMemoryEmbeddingStore();

        DocumentSplitter ds = DocumentSplitters.recursive(500,100);

        EmbeddingStoreIngestor embeddingStoreIngestor = EmbeddingStoreIngestor.builder()
                .embeddingStore(embeddingStore)
                .embeddingModel(embeddingModel)
                .documentSplitter(ds)
                .build();
        embeddingStoreIngestor.ingest(documents);
        return embeddingStore;
    }

    @Bean("firstContentRetriever")
    public ContentRetriever contentRetriever(EmbeddingStore firstEmbeddingStore){
        return EmbeddingStoreContentRetriever.builder()
                .embeddingStore(firstEmbeddingStore)
                .embeddingModel(embeddingModel)
                .maxResults(3)
                .minScore(0.5)
                .build();
    }


}
